package com.leyou.service.impl;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entity.Category;
import com.leyou.mapper.ICategoryMapper;
import com.leyou.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author xiuer
 * @description 商品分类业务层
 * @create 2019/6/23 18:56
 */
@Service
public class CategoryServiceImpl implements ICategoryService {

    @Autowired
    private ICategoryMapper categoryMapper;

    /**
     * 根据父pid查询子商品分类目录
     */
    @Override
    public List<CategoryDTO> queryCategoryByPid(Long pid) {
        List<Category> categories = categoryMapper.queryCategoryByPid(pid);
        if (CollectionUtils.isEmpty(categories)) {
            //无结果 返回404
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(categories, CategoryDTO.class);
    }

    /**
     * 根据品牌id查询商品所有分类
     *
     * @param bid
     * @return
     */
    @Override
    public List<CategoryDTO> queryCategoryByBid(Long bid) {
        List<Category> categories = categoryMapper.queryCategoryByBid(bid);
        if (CollectionUtils.isEmpty(categories)) {
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(categories, CategoryDTO.class);
    }

    /**
     * 根据分类id集合查询所有分类
     *
     * @param cids 分类id集合
     */
    @Override
    public List<CategoryDTO> queryCategoryByCids(List<Long> cids) {

        List<Category> list = categoryMapper.queryCategoryByCids(cids);
        if (CollectionUtils.isEmpty(list)) {
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list, CategoryDTO.class);
    }
}
